package com.jalonjia.seckill.core.mq;

import com.alibaba.fastjson.JSON;
import com.jalonjia.seckill.core.entity.SeckillOrder;
import com.jalonjia.seckill.core.service.SeckillOrderService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;

@Slf4j
@Component
@RequiredArgsConstructor
public class RabbitQueueListener {
	private final SeckillOrderService seckillOrderService;

	/**
	 * 监听 hello 队列的处理器
	 */
	@RabbitListener(queues = "saveOrder")
	@RabbitHandler
	public void saveOrder(Message message) {
		SeckillOrder order = JSON.parseObject(message.getPayload().toString(), SeckillOrder.class);
		log.info("消费MQ saveOrder: " + order);
		seckillOrderService.saveOrder(order);
	}
}